/*   
 * Copyright (c) 2016-2020 canaanQd. All Rights Reserved.   
 *   
 * This software is the confidential and proprietary information of   
 * canaanQd. You shall not disclose such Confidential Information   
 * and shall use it only in accordance with the terms of the agreements   
 * you entered into with canaanQd.   
 *   
 */ 

package com.lg.system.user.service.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.lg.system.user.dao.UserDao;
import com.lg.system.user.entity.UserEntity;
import com.lg.system.user.entity.UserRole;
import com.lg.system.user.service.UserRoleService;
import com.lg.system.user.service.UserService;
import com.lg.utils.MD5Utils;
import com.lg.utils.R;

@Service
//@Transactional
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService  {

	@Autowired
	UserRoleService userRoleService;
	
	@Override
	public boolean save(UserEntity userEntity) {
		userEntity.setGmtCreate(new Date());
		userEntity.setPassword(MD5Utils.encrypt(userEntity.getUsername(), userEntity.getPassword()));
		List<UserRole> list=new ArrayList<UserRole>();
		if( super.insert(userEntity)){
			for(Long roleId :userEntity.getRoleIds()){
				UserRole userRole=new UserRole();
				userRole.setRoleId(roleId);
				userRole.setUserId(userEntity.getUserId());
				list.add(userRole);
			}
		}
		return userRoleService.insertBatch(list);
	}

	@Override
	public boolean update(UserEntity userEntity) {
		userEntity.setGmtModified(new Date());
		List<UserRole> list=new ArrayList<UserRole>();
		if( super.updateById(userEntity)){
			//根据用户id先删除所有的角色然后再分配
			Map<String, Object> param=new HashMap<>();
			param.put("user_id", userEntity.getUserId());
			if(userRoleService.deleteByMap(param)){
				for(Long roleId :userEntity.getRoleIds()){
					UserRole userRole=new UserRole();
					userRole.setRoleId(roleId);
					userRole.setUserId(userEntity.getUserId());
					list.add(userRole);
				}
			}
		}
		return userRoleService.insertBatch(list);
	}

	@Override
	public boolean deleteById(Long userId) {
		//先删除用户角色关系表再删除用户
		if(userRoleService.deleteByUserId(userId)){
			return super.deleteById(userId);
		}else{
			return false;
		}
	}

	@Override
	public boolean deleteByIds(List<Long> ids) {
		if(ids==null||ids.isEmpty()){
			return false;
		}
		for(Long userId:ids){
			userRoleService.deleteByUserId(userId);
		}
		return super.deleteBatchIds(ids);
	}

	@Override
	public UserEntity selectDetailById(Long userId) {
		UserEntity user=selectById(userId);
		List<Long> roleIds=userRoleService.selectRoleIdByUId(userId);
		Long[] roleArr = new Long[roleIds.size()];
		user.setRoleIds(roleIds.toArray(roleArr));
		return user;
	}

}
